package self.hoy.demo.rabbitmq.route;

import com.rabbitmq.client.*;
import self.hoy.demo.rabbitmq.Constant;
import self.hoy.demo.rabbitmq.util.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hyh
 * @date 2020/6/7
 */
public class ErrorReceive implements Constant {
    private static final String QUEUE_Name = "test_queue_direct_error";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(DIRECT_EXCHANGE_NAME, TYPE_DIRECT);
        channel.queueDeclare(QUEUE_Name, false, false, false, null);

        channel.queueBind(QUEUE_Name, DIRECT_EXCHANGE_NAME, "error");
        channel.queueBind(QUEUE_Name, DIRECT_EXCHANGE_NAME, "info");
        channel.queueBind(QUEUE_Name, DIRECT_EXCHANGE_NAME, "debug");

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) {
                System.out.println("direct error receive : " + new String(body));
            }
        };
        channel.basicConsume(QUEUE_Name, true, consumer);
    }
}
